<?php
/**
 * @file $HeadURL: imagelibrary.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2014-12-15 下午12:13:39 $
 * @brief 图片库
 */
namespace haibao\erp\data;

use \haibao\frame\data\query\Condition;
use \haibao\erp\model\filter\ImageLibrary as ImageLibraryFilter;

class ImageLibrary extends \haibao\erp\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\erp\model\data\ImageLibrary');
	}
	
	/**
	 * 存数据库
	 * @param string $file
	 * @param string $storagePath
	 * @param int $userId
	 * @param int $upSource
	 * @param boolean $isTemp 是否临时
	 * @return int picId 图片id
	 */
	public function saveImage($uploadUrl, $storagePath){
		$model = new \haibao\erp\model\data\imagelibrary\ImageLibrary();

	    $model->originalUrl = $uploadUrl;
	    $model->createTime = new \DateTime();
	    
	    $this->add($model);
	    return $model->Id;
	}
	
	public function getImageById($imageId){
	    $imageId = (array)$imageId;
	
	    $query = new \haibao\erp\model\filter\imagelibrary\ImageLibrary();
	    $query->where(\haibao\erp\model\filter\imagelibrary\ImageLibrary::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $imageId);
	
	    return $this->getAll($query);
	}
	
	
	public function getImageByFilename($filename){
		$sql = 'select Id from ImageLibrary where originalUrl=%s limit 1';
		return $this->query($sql, array($filename))->fetch_assoc();
	}

}